<?php
/**
 * 遍历 => 替换递归
 */
function getInfinitus($data,$children = 'children')
{
    if (is_array($data) && !empty($data)) {
        $items = [];
        foreach ($data as  $v) {
            $items[$v['id']] = $v;
        }

        $tree = array();
        foreach ($items as $item) {
            if (isset($items[$item['pid']])) {
                $items[$item['pid']][$children][] = &$items[$item['id']];
            } else {
                $tree[] = &$items[$item['id']];
            }
        }
        return $tree;
    }

    return  [];
}